package dancrawler.server.gamecmd;

import dancrawler.common.Misc;
import dancrawler.proto.cmd.CmdHandler;
import dancrawler.server.GameHandler;
import dancrawler.server.GameObject;
import dancrawler.server.Gameworld;
import dancrawler.server.Player;
import dancrawler.server.Portal;
import dancrawler.server.Tile;
import dancrawler.server.WorldGenerator;
import dancrawler.server.netMsg;

/**
 *
 */
public class CmdActivatePortal extends CmdHandler{
    protected int[] args = {2,2};
    private GameHandler gh;
    
    public CmdActivatePortal(GameHandler gh)
    {
        this.gh=gh;
    }
    
    @Override
    public void handleCmd(netMsg msg) {
        int x = gh.getPlayer(msg.getSender().getId()).getPc().getX();
        int y = gh.getPlayer(msg.getSender().getId()).getPc().getY();
        Player pl = gh.getPlayer(msg.getSender().getId());
        if(pl != null)
        {
            Gameworld gw = pl.getPc().getWorld();
            Tile t = gw.getTile(x, y);
            if(t==gw.getTile(pl.getPc().getX(), pl.getPc().getY()))
            {
                Portal p = null;
                for(GameObject o:t.getContents())
                {
                    if(o instanceof Portal)
                    {
                        p = (Portal)o;
                    }
                }
                if(p != null && p.getTarget() !=gh.getTopLevel())
                {
                    
                    Tile target = p.getTarget();
                    Gameworld tw;
                    if(target == null)
                    {
                        tw = new WorldGenerator(gh.getTopLevel().getObjectRegister(),gh,gw.getDepth()+1);
                        tw.setUpdateList(gw.getTileUpdates());
                        target = tw.getStartTile();
                        p.setTarget(target);
                        Portal p2 = new Portal("stairUP", tw);
                        p2.setTarget(t);
                        target.add(p2);
                    }
                    else
                    {
                        tw = target.getWorld();
                    }
                    
                    gw.removeObject(pl.getPc());
                    target.add(pl.getPc());
                    pl.getPc().setWorld(tw);
                    gh.sendWorld(msg.getSender());
                    for(Player pl2 : gh.getPlayers())
                    {
                        if(pl2.getPc().getWorld()==tw && pl2!=pl)
                        {
                            netMsg msg2 = new netMsg((short)1, (short)0, null);
                            byte[][] args = new byte[3][];
                            args[0] =Misc.shortToBytes((short)target.getX());
                            args[1] =Misc.shortToBytes((short)target.getY());
                            args[2] =Misc.shortToBytes((short)pl.getPc().getId());
                            msg2.setArgs(args);
                            msg2.setFinalized(true);
                            pl2.getCon().getOutQueue().offer(msg2);
                            
                        }
                    }
                }
            }
        }
    }

    @Override
    public int getId() {
        return 23;
    }

    @Override
    public CmdHandler getHandler() {
        return this;
    }
    
}
